package com.study;

import com.study.dto.NamesOnly;
import com.study.dto.StuInferDTO;
import com.study.dto.StuLomDTO;
import com.study.dto.StudentDTO;
import com.study.repository.StudentRepository;
import com.study.utils.BeanJpaUtils;
import java.util.Collection;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@Slf4j
@SpringBootTest
public class ConsumEntitySql {

    @Autowired
    private StudentRepository studentRepository;

    @Test
    public void listDTO() {
        //Spring Data JPA 投影查询
        List<StuInferDTO> studentDTOS = studentRepository.listDTO();
        System.out.println(studentDTOS);
        List<StudentDTO> list = BeanJpaUtils.copyList(studentDTOS,StudentDTO::new);

        System.out.println(list);
        //这个也不能用
        /*Collection<StudentDTO> stuLomDTOS = studentRepository.listStuDTO(StudentDTO.class);
        System.out.println(studentDTOS);*/
    }

    @Test
    public void listNameOnly() {
        // TODO 这个也不能用
        List<NamesOnly> namesOnlies = studentRepository.listNameOnly();
        System.out.println(namesOnlies);
    }

    @Test
    public void list(){
        //这个也不能用
        List<StudentDTO> list = studentRepository.listStuDTO(StudentDTO.class);
        System.out.println(list);
    }
    /**
     * 动态投影
     */
    @Test
    public void dayList() {
        List<StuInferDTO> stuInferDTOS = studentRepository.listDTO( );
        System.out.println(stuInferDTOS);

        List<StudentDTO> list = Lists.newArrayList();
        stuInferDTOS.stream().forEach(item->{
            System.out.println( item.getBirthTime());
            StudentDTO studentDTO = new StudentDTO();
            BeanUtils.copyProperties(item,studentDTO);
            System.out.println(studentDTO);
            list.add(studentDTO);
        });

        System.out.println(list);
    }
}
